using System;
using System.Collections;
using Framework.Library.Base.Base;
using Framework.Library.Core.Provider.Caches;
using System.Web.UI.WebControls;
using System.IO;
using Framework.Library.Core.Config;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL.JObject;
using System.Collections.Generic;

namespace Framework.Portal.Layouts.Admin.Components.Caches
{
    public partial class CacheManager : BaseControl
    {
        public string MENUNAME = string.Empty;

        public CacheManager()
        {
        }

        public CacheManager(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            lblCommandName.Text = MENUNAME;
            if(!IsPostBack)
            {
                GetCaches();
            }
        }

        protected void btnRemoveCache_Click(object sender, EventArgs e)
        {
            if (lstAllCache.Items.Count > 0)
            {
                foreach (ListItem item in lstAllCache.Items)
                {
                    if (item.Selected)
                    {
                        string key = item.Value.Split(';')[0].Split(':')[1];
                        string param = item.Value.Split(';')[1].Split(':')[1];
                        Caching.Data.Invalidate(key, param);
                    }
                }
                GetCaches();
            }
        }

        protected void btnRemoveAll_Click(object sender, EventArgs e)
        {
            Caching.Data.Invalidate();
            GetCaches();
        }

        private void GetCaches()
        {
            lstAllCache.Items.Clear();
            List<string> lstHashCache = Caching.Data.GetAll();
            if (lstHashCache != null)
            {
                foreach (string item in lstHashCache)
                {
                    lstAllCache.Items.Add(new ListItem(item, item));
                }
            }
        }
    }
}